################################
#
#     Makefile BIBLIO (progetto lso 2012)
#     (fram 1)(fram 2)(fram 3)
################################

# ***** DA COMPLETARE ******  con i file da consegnare *.c e *.h     
# primo frammento                                                                                                           
FILE_DA_CONSEGNARE1=bib.c

# secondo frammento                                                                                                         
FILE_DA_CONSEGNARE2=bibaccess

# terzo frammento                                                                                                          
FILE_DA_CONSEGNARE3=

# Compilatore
CC= gcc
# Flag di compilazione
CFLAGS = -Wall -pedantic -g #$(COVFLAGS)

# Librerie 
# Directory in cui si trovano le librerie
LIBDIR = ../lib
# Opzione di linking
LIBS = -L $(LIBDIR)

# Nome libreria progetto
LIBNAME1 = libbiblio.a
# Oggetti libreria $(LIBNAME1)
# DA COMPLETARE se si usano piu' file in aggiunta da dgraph.o
objects1 = bib.o

# Nome eseguibili primo frammento
EXE1=tibib
EXE2=tibib2


.PHONY: clean lib test11 test12 consegna1 docu
.PHONY: test21 consegna2
.PHONY: test31 test32 test33 test34 consegna3


# creazione libreria 
lib:  $(objects1)
	-rm  -f $(LIBDIR)/$(LIBNAME1)
	ar -r $(LIBNAME1) $(objects1)
	cp $(LIBNAME1) $(LIBDIR)


# eseguibili test primo frammento
$(EXE1): test-bib.o
	$(CC)  -o $@  $^ $(LIBS) -lbiblio

$(EXE2): test2-bib.o
	$(CC)  -o $@  $^ $(LIBS) -lbiblio

test-bib.o: test-bib.c bib.h
	$(CC) $(CFLAGS) -c $<

test2-bib.o: test2-bib.c bib.h
	$(CC) $(CFLAGS) -c $<

#make rule per gli altri .o del primo frammento (***DA COMPLETARE***)

######### target test libreria comunicazione (test31)

testserv: testserv.o  comsock.o 
	$(CC) -o $@ $^  

testcli: testcli.o  comsock.o 
	$(CC) -o $@ $^   

testserv.o: testserv.c comsock.h  
	$(CC) $(CFLAGS) -c $<	 

testcli.o: testcli.c comsock.h   
	$(CC) $(CFLAGS) -c $<	 


######### target server e client progetto biblio frammento 3 (***DA COMPLETARE***




# make rule per gli altri .o del terzo frammento (***DA COMPLETARE***)




########### NON MODIFICARE DA QUA IN POI ################
# genera la documentazione con doxygen
docu: ../doc/Doxyfile
	make -C ../doc/

#ripulisce  l'ambiente
clean:
	-rm *.o


test11: 
	make clean
	make lib
	make $(EXE1)
	-rm -fr ./bibtrace.log
	MALLOC_TRACE=./bibtrace.log ./$(EXE1)
	mtrace ./$(EXE1) ./bibtrace.log
	@echo "********** Test11 superato!"

test12: 
	make clean
	make lib
	make $(EXE2)
	-rm -fr ./bibtrace.log
	cp DATA/file_bib1 DATA/file_bib2 .
	cp DATA/file_bib4.out.check.autore DATA/file_bib4.out.check.titolo .
	chmod 644 ./file_bib*
	MALLOC_TRACE=./bibtrace.log ./$(EXE2)
	mtrace ./$(EXE2) ./bibtrace.log
	diff file_bib2.out file_bib3.out 
	@echo "********** Test12 superato!"

# test secondo frammento
test21:
	cp DATA/*.log .            
	cp DATA/out.bibaccess.check .            
	chmod 644 ./*.log
	chmod 644 ./out.bibaccess.check
	./testscript >out.bibaccess
	diff ./out.bibaccess ./out.bibaccess.check  
	@echo "********** Test21 superato!"


test22:
	cp DATA/*.log2.* .            
	cp DATA/*.log3.* .            
	cp DATA/out2.bibaccess.check .            
	chmod 644 ./*.log2.*
	chmod 644 ./*.log3.*
	chmod 644 ./out2.bibaccess.check
	./testscript2 >out2.bibaccess
	diff ./out2.bibaccess ./out2.bibaccess.check  
	@echo "********** Test22 superato!"

# test terzo frammento
test31:
	make clean
	make lib
	make testserv
	make testcli
	cp DATA/out.cli.check .
	chmod 644 out.cli.check
	rm -fr ./tmp
	mkdir ./tmp
	MALLOC_TRACE=./bibtrace.log ./testserv &
	MALLOC_TRACE=./bibtrace1.log ./testcli > out.cli
	mtrace ./testcli ./bibtrace1.log 
	mtrace ./testserv ./bibtrace.log 
	diff out.cli out.cli.check
	@echo "********** Test31 superato!"

test32:
	make clean
	make lib
	make $(exeserv)
	make $(execli)
	cp DATA/*.log.check .
	chmod 644 *log.check
	cp DATA/*.bib .
	chmod 644 *bib
	rm -fr ./tmp
	mkdir ./tmp
	./testseq
	@echo "********** Test32 superato!"

test33:
	make clean
	make lib
	make $(exeserv)
	make $(execli)
	cp DATA/*.log2.check .
	chmod 644 *log2.check
	cp DATA/*.bib .
	chmod 644 *.bib
	rm -fr ./tmp
	mkdir ./tmp
	./testpar1
	@echo "********** Test33 superato!"

test34:
	make clean
	make lib
	make $(exeserv)
	make $(execli)
	cp DATA/*.log3.check .
	chmod 644 *log3.check
	cp DATA/*.log4.check .
	chmod 644 *log4.check
	cp DATA/*.bib .
	chmod 644 *.bib
	cp DATA/out3.bibaccess.check .
	chmod 644 out3.bibaccess.check
	rm -fr ./tmp
	mkdir ./tmp
	./testpar2
	@echo "********** Test34 superato!"

# target di consegna del primo frammento
SUBJECT1="lso12: consegna primo frammento"
ADDRESS="susanna@di.unipi.it"

#effettua i test e prepara il tar per la consegna
consegna1:
	make clean
	make test11
	make test12
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f1.tar ./gruppo.txt $(FILE_DA_CONSEGNARE1) ./Makefile
	@echo -e "*** PRIMO FRAMMENTO: TAR PRONTO $(USER)-f1.tar ***"
	@echo -e "*** inviare per email a $(ADDRESS) con subject\n \"$(SUBJECT1)\" ***"

# target di consegna del secondo frammento
SUBJECT2="lso12: consegna secondo frammento"
ADDRESS="susanna@di.unipi.it"

#effettua i test e prepara il tar per la consegna
consegna2:
	make clean
	make test21
	make test22
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f2.tar ./gruppo.txt $(FILE_DA_CONSEGNARE2)
	@echo "*** SECONDO FRAMMENTO: TAR PRONTO $(USER)-f2.tar ***"
	@echo "*** inviare per email a $(ADDRESS) con subject\n \"$(SUBJECT2)\" ***"


# target di consegna del terzo frammento
SUBJECT3="lso12: consegna terzo frammento"
ADDRESS="susanna@di.unipi.it"

#effettua i test e prepara il tar per la consegna
consegna3:
	make clean
	make test31
	make test32
	make test33
	make test34
	./gruppo-check.pl < gruppo.txt
	tar -cvf $(USER)-f3.tar ./gruppo.txt $(FILE_DA_CONSEGNARE3) ./Makefile
	@echo "*** TERZO FRAMMENTO: TAR PRONTO $(USER)-f3.tar ***"
	@echo "*** inviare per email a $(ADDRESS) con subject\n \"$(SUBJECT3)\" ***"

